<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 1/19/15
 * Time: 1:42 PM
 */
namespace MyApp\Application;

use MyApp\Application\Controller;
use MyApp\Application\Controller\IndexController;
use MyApp\Application\Controller\StaticController;
use MyApp\Application\Controller\ErrorController;
use DinhThi\MVC\AbstractModule;
use DinhThi\MVC\Configuration;
use DinhThi\MVC\Request;
class Module extends  AbstractModule{
    public function __construct(){
        parent::__construct();

    }

    public function dispatcher($controller, $action){
        $controller->$action();
    }

    public function onBootstrap(){
        if ((Configuration::getInstance()->getProperty('MAINTAIN'))){
            $this->Maintain();
        }
        /**
         * @var Request $request
         */
        $request = Request::getInstance();
        $params = $request->getParams();
        //Home page
        if (empty($params)){
            $controller = new IndexController();
            $controller->indexAction();
        } else{
            $className = $params[0] . 'Controller';
            $module = (Configuration::getInstance()->getProperty('MODULE'));

            $class = "\\MyApp\\$module\\Controller\\$className";
            if (class_exists($class)){
                $controller = new $class();
                if (isset ($params[1]) && !empty($params[1])){
                    if (method_exists($controller, $params[1] . 'Action')){
                        //Call class and method
                        $this->dispatcher($controller, $params[1] . 'Action');
                    } else{
                        //Call defaultAction of controller
                        if (method_exists($controller, 'defaultMethod')){
                            $this->dispatcher($controller, 'defaultMethod');
                        } else{
                            $this->pageNotFound();
                        }
                    }
                } else{
                    //Call indexAction if url has only controller name
                    $this->dispatcher($controller, 'IndexAction');
                }
            } else{
                $controller = new StaticController();
                $actionName = $params[0] . 'Action';
                if (method_exists($controller, $actionName)){
                    //Call action in staticController
                    $this->dispatcher($controller, $actionName);
                } else{
                    $this->pageNotFound();
                }
            }
        }
    }

    public function pageNotFound(){
        $controller = new ErrorController();
        $controller->pageNotFound();
        exit;
    }

    public function notAllowPage(){
        $controller = new ErrorController();
        $controller->notAllowPage();
        exit;
    }

    public function customErrorPage(){
        $controller = new ErrorController();
        $controller->customErrorPage();
        exit;
    }

    public function Maintain(){
        $controller = new Controller\MaintainController();
        $controller->Maintain();
        exit;
    }
} 